<?php
class Request {

	var $attributes = array();

	function getAttribute($name) {
		if( array_key_exists($name, $this->attributes) )
		return $this->attributes[$name];	// attributes.get(name)
		else
		return NULL;
	}

	function setAttribute($name, $value) {

		// Name cannot be null
		if($name == NULL) {
			return;
		}

		// Null value is the same as removeAttribute() !!!!!!!!!!
		if($value === NULL) {
			$this->removeAttribute($name);
			return;
		}

		$this->attributes[$name] = $value; // attributes.put(name, value)
	}

	function zapArrayElement($key, &$array) {

		$idx = array_search( $key, array_keys($array) );

		if( $idx === NULL || $idx === False) {
			return False; // Not found, nothing to do
		} else {
			array_splice( $array, $idx, 1);
			return True;
		}
	}

	function removeAttribute($name) {

		// Remove the specified element from the attributes array, including the key.
		$this->zapArrayElement($name, $this->attributes);

	}

	function setAttributeArray($Arr) { //sets attributes of array assoc to respective values
       
		if(is_array($Arr)) {
			foreach ($Arr as $key=>$val) {       
				$this->setAttribute("$key", "$val");
			}
		}
	}

	function redirect($do='', $action='', $params='') {
        $url = $this->createURL($do, $action, $params);
		header("Location:".$url);
	}
	
	function createURL($do='', $action='', $params='') {
        
		$url = SITE_PATH;
		
		if(REWRITE_URL) {
		    
		    $paramsArr = array();
		    if($do) $paramsArr[] = $do;
		    if($action) $paramsArr[] = $action;
		    if($params!='') {
		        $temp = split("[&=]", $params);
		        if($temp) {
		            $paramsArr = array_merge($paramsArr, $temp);
		        }
		    }
		    $url = $url.implode("/", $paramsArr);
		}
		else {
		    $url = ($do)?$url.CONTROLLER."?do=$do":$url;
		    $url = ($action)?$url."&action=$action":$url;
		    $url = ($params)?"$url&$params":$url;
		}
		return $url;
	}
}
?>